package com.zero.mms.controller;

import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.neusoft.shry.dto.EmpDto;
import com.neusoft.shry.service.EmpService;
import com.neusoft.shry.service.PositionService;
import com.zero.basic.exception.SmsErrorConstant;
import com.zero.basic.exception.SmsRuntimeException;
import com.zero.basic.exception.SmsStatusCode;
import com.zero.basic.model.Pager;
import com.zero.basic.util.JsonCreator;
import com.zero.mms.model.Employee;
import com.zero.mms.model.Position;

@Controller("empController")
@RequestMapping("emp")
public class EmpController {
	
	@Inject
	private EmpService empService;
	
	@Inject
	private PositionService positionService;
	
	@RequestMapping("/searchEmp")
	@ResponseBody
	public Object getEmp(String emp_id_string,String emp_name,String dep_name){
		ObjectNode objectNode = JsonCreator.generateJsonObj();
		
		Integer emp_id;
		if("*".equals(emp_id_string)||"".equals(emp_id_string)){
			emp_id = null;
		}else{
			emp_id = Integer.parseInt(emp_id_string);
		}
		
		try {
			Pager<EmpDto> pager = empService.getEmpByConditions(emp_id, emp_name, dep_name);
			objectNode.putPOJO("response", pager.getDatas());
			objectNode.put("page_num", pager.getOffset()/pager.getSize()+1);
			objectNode.put("total_page", (pager.getTotal()%pager.getSize())>0?pager.getTotal()/pager.getSize()+1:pager.getTotal()/pager.getSize());
			objectNode.put("total_num", pager.getTotal());
			objectNode.put("page_size", pager.getSize());
			System.out.println(objectNode);
		} catch (SmsRuntimeException e) {
			objectNode.put("message", e.getErrorDescription());
			objectNode.put("error", e.getError());
			objectNode.put("messageType", e.getStatusCode());
		} catch (Exception e) {
			e.printStackTrace();
			objectNode.put("message", SmsErrorConstant.SERVER_ERROR_MSG);
			objectNode.put("error", SmsErrorConstant.WEB_APPLICATION);
			objectNode.put("messageType", SmsStatusCode.SERVER_ERROR);
		}
		return objectNode;
	}
	
	@RequestMapping("/addEmp")
	@ResponseBody
	public Object addEmp(String empName,String empSex,String empBirth,String empIdentity,String empJoinDate,String empWorkDate,String empType,String empSource,
			String empPolStatus,String empMinzu,String empJiguan,String empPhone,String empEmail,String empHeight,String empBlood,String empMarriageType,
			String empBirthPlace,String empHukouPlace,String empXueli,String empXuewei,String empGraduateSchool,String empMajor,String empGraduateDate,
			String depId,String positionId){
		ObjectNode objectNode = JsonCreator.generateJsonObj();
		
		Integer emp_sex;
		Integer dep_id;
		Integer position_id;
		
		if("*".equals(empSex)||"".equals(empSex)){
			emp_sex = null;
		}else{
			emp_sex = Integer.parseInt(empSex);
		}
		if("*".equals(depId)||"".equals(depId)){
			dep_id = null;
		}else{
			dep_id = Integer.parseInt(depId);
		}
		if("*".equals(positionId)||"".equals(positionId)){
			position_id = null;
		}else{
			position_id = Integer.parseInt(positionId);
		}
		
		Employee emp = new Employee();
		emp.setEmpName(empName);
		emp.setEmpSex(emp_sex);
		emp.setEmpBirth(empBirth);
		emp.setEmpIdentity(empIdentity);
		emp.setEmpJoinDate(empJoinDate);
		emp.setEmpWorkDate(empWorkDate);
		emp.setEmpType(empType);
		emp.setEmpSource(empSource);
		emp.setEmpPolStatus(empPolStatus);
		emp.setEmpMinzu(empMinzu);
		emp.setEmpJiguan(empJiguan);
		emp.setEmpPhone(empPhone);
		emp.setEmpEmail(empEmail);
		emp.setEmpHeight(empHeight);
		emp.setEmpBlood(empBlood);
		emp.setEmpMarriageType(empMarriageType);
		emp.setEmpBirthPlace(empBirthPlace);
		emp.setEmpHukouPlace(empHukouPlace);
		emp.setEmpXueli(empXueli);
		emp.setEmpXuewei(empXuewei);
		emp.setEmpGraduateSchool(empGraduateSchool);
		emp.setEmpMajor(empMajor);
		emp.setEmpGraduateDate(empGraduateDate);
		emp.setDepId(dep_id);
		emp.setPositionId(position_id);
		
		try {
			System.out.println(emp);
			empService.addEmp(emp);
			objectNode.putPOJO("response", "");
			objectNode.put("message", "添加员工成功");
			objectNode.put("messageType", SmsStatusCode.SUC_CODE);
			System.out.println(objectNode);
		} catch (SmsRuntimeException e) {
			objectNode.put("message", e.getErrorDescription());
			objectNode.put("error", e.getError());
			objectNode.put("messageType", e.getStatusCode());
		} catch (Exception e) {
			e.printStackTrace();
			objectNode.put("message", SmsErrorConstant.SERVER_ERROR_MSG);
			objectNode.put("error", SmsErrorConstant.WEB_APPLICATION);
			objectNode.put("messageType", SmsStatusCode.SERVER_ERROR);
		}
		return objectNode;
	}
	
	@RequestMapping("/deleteEmp")
	@ResponseBody
	public Object deleteEmp(String emp_id_string){
		ObjectNode objectNode = JsonCreator.generateJsonObj();
		
		Integer emp_id;
		if("*".equals(emp_id_string)||"".equals(emp_id_string)){
			emp_id = null;
		}else{
			emp_id = Integer.parseInt(emp_id_string);
		}
		
		try {
			empService.deleteEmp(emp_id);
			objectNode.putPOJO("response", "");
			objectNode.put("message", "删除岗位成功");
			objectNode.put("messageType", SmsStatusCode.SUC_CODE);
			System.out.println(objectNode);
		} catch (SmsRuntimeException e) {
			objectNode.put("message", e.getErrorDescription());
			objectNode.put("error", e.getError());
			objectNode.put("messageType", e.getStatusCode());
		} catch (Exception e) {
			e.printStackTrace();
			objectNode.put("message", SmsErrorConstant.SERVER_ERROR_MSG);
			objectNode.put("error", SmsErrorConstant.WEB_APPLICATION);
			objectNode.put("messageType", SmsStatusCode.SERVER_ERROR);
		}
		
		return objectNode;
	}
	
	@RequestMapping("/updateEmp")
	@ResponseBody
	public Object updateEmp(String empId,String empName,String empSex,String empBirth,String empIdentity,String empJoinDate,String empWorkDate,String empType,
			String empSource,String empPolStatus,String empMinzu,String empJiguan,String empPhone,String empEmail,String empHeight,String empBlood,
			String empMarriageType,String empBirthPlace,String empHukouPlace,String empXueli,String empXuewei,String empGraduateSchool,String empMajor,
			String empGraduateDate,String depId,String positionId){
		ObjectNode objectNode = JsonCreator.generateJsonObj();
		
		Integer emp_id;
		Integer emp_sex;
		Integer dep_id;
		Integer position_id;
		
		if("*".equals(empId)||"".equals(empId)){
			emp_id = null;
		}else{
			emp_id = Integer.parseInt(empId);
		}
		if("*".equals(empSex)||"".equals(empSex)){
			emp_sex = null;
		}else{
			emp_sex = Integer.parseInt(empSex);
		}
		if("*".equals(depId)||"".equals(depId)){
			dep_id = null;
		}else{
			dep_id = Integer.parseInt(depId);
		}
		if("*".equals(positionId)||"".equals(positionId)){
			position_id = null;
		}else{
			position_id = Integer.parseInt(positionId);
		}
		
		Employee emp = new Employee();
		emp.setEmpId(emp_id);
		emp.setEmpName(empName);
		emp.setEmpSex(emp_sex);
		emp.setEmpBirth(empBirth);
		emp.setEmpIdentity(empIdentity);
		emp.setEmpJoinDate(empJoinDate);
		emp.setEmpWorkDate(empWorkDate);
		emp.setEmpType(empType);
		emp.setEmpSource(empSource);
		emp.setEmpPolStatus(empPolStatus);
		emp.setEmpMinzu(empMinzu);
		emp.setEmpJiguan(empJiguan);
		emp.setEmpPhone(empPhone);
		emp.setEmpEmail(empEmail);
		emp.setEmpHeight(empHeight);
		emp.setEmpBlood(empBlood);
		emp.setEmpMarriageType(empMarriageType);
		emp.setEmpBirthPlace(empBirthPlace);
		emp.setEmpHukouPlace(empHukouPlace);
		emp.setEmpXueli(empXueli);
		emp.setEmpXuewei(empXuewei);
		emp.setEmpGraduateSchool(empGraduateSchool);
		emp.setEmpMajor(empMajor);
		emp.setEmpGraduateDate(empGraduateDate);
		emp.setDepId(dep_id);
		emp.setPositionId(position_id);
		
		try {
			System.out.println("airghaoergbig"+emp);
			empService.updateEmp(emp);
			objectNode.putPOJO("response", "");
			objectNode.put("message", "修改员工成功");
			objectNode.put("messageType", SmsStatusCode.SUC_CODE);
			System.out.println(objectNode);
		} catch (SmsRuntimeException e) {
			objectNode.put("message", e.getErrorDescription());
			objectNode.put("error", e.getError());
			objectNode.put("messageType", e.getStatusCode());
		} catch (Exception e) {
			e.printStackTrace();
			objectNode.put("message", SmsErrorConstant.SERVER_ERROR_MSG);
			objectNode.put("error", SmsErrorConstant.WEB_APPLICATION);
			objectNode.put("messageType", SmsStatusCode.SERVER_ERROR);
		}
		return objectNode;
	}
	
	@RequestMapping("/viewEmp")
	@ResponseBody
	public Object viewEmp(String emp_id_string){
		ObjectNode objectNode = JsonCreator.generateJsonObj();
		
		Integer emp_id;
		if("*".equals(emp_id_string)||"".equals(emp_id_string)){
			emp_id = null;
		}else{
			emp_id = Integer.parseInt(emp_id_string);
		}
		
		try {
			Employee employee = empService.getEmpById(emp_id);
			objectNode.putPOJO("response", employee);
			System.out.println(objectNode);
		} catch (SmsRuntimeException e) {
			objectNode.put("message", e.getErrorDescription());
			objectNode.put("error", e.getError());
			objectNode.put("messageType", e.getStatusCode());
		} catch (Exception e) {
			e.printStackTrace();
			objectNode.put("message", SmsErrorConstant.SERVER_ERROR_MSG);
			objectNode.put("error", SmsErrorConstant.WEB_APPLICATION);
			objectNode.put("messageType", SmsStatusCode.SERVER_ERROR);
		}
		
		return objectNode;
	}
	
	@RequestMapping("/getPosSelectList")
	@ResponseBody
	public Object getSelectList(String dep_id_string){
		ObjectNode objectNode = JsonCreator.generateJsonObj();
		
		Integer dep_id;
		if("*".equals(dep_id_string)||"".equals(dep_id_string)){
			dep_id = null;
		}else{
			dep_id = Integer.parseInt(dep_id_string);
		}
		
		try {
			List<Position> positions = empService.getDepPos(dep_id);
			objectNode.putPOJO("posList", positions);
			System.out.println(objectNode);
		} catch (SmsRuntimeException e) {
			objectNode.put("message", e.getErrorDescription());
			objectNode.put("error", e.getError());
			objectNode.put("messageType", e.getStatusCode());
		} catch (Exception e) {
			e.printStackTrace();
			objectNode.put("message", SmsErrorConstant.SERVER_ERROR_MSG);
			objectNode.put("error", SmsErrorConstant.WEB_APPLICATION);
			objectNode.put("messageType", SmsStatusCode.SERVER_ERROR);
		}
		return objectNode;
	}
	
	@RequestMapping("/getPositionList")
	@ResponseBody
	public Object getPositionList(){
		ObjectNode objectNode = JsonCreator.generateJsonObj();
		
		try {
			List<Position> positions = positionService.getAllPositions();
			objectNode.putPOJO("posList", positions);
			System.out.println(objectNode);
		} catch (SmsRuntimeException e) {
			objectNode.put("message", e.getErrorDescription());
			objectNode.put("error", e.getError());
			objectNode.put("messageType", e.getStatusCode());
		} catch (Exception e) {
			e.printStackTrace();
			objectNode.put("message", SmsErrorConstant.SERVER_ERROR_MSG);
			objectNode.put("error", SmsErrorConstant.WEB_APPLICATION);
			objectNode.put("messageType", SmsStatusCode.SERVER_ERROR);
		}
		return objectNode;
	}

}
